package com.gome.oa.ess;

import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;

/**
 *
 * @author: tang li
 * @date: 2016年8月21日
 * @desc: 
 *
 */
public class Http {

	private static HttpClientContext context = HttpClientContext.create();
	
	public static CloseableHttpResponse post(String url, List<NameValuePair> list) throws Exception {
		CloseableHttpClient client = HttpClients.custom()
				.setRedirectStrategy(new DefaultRedirectStrategy() {
					public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
						return false;
					}
				})
				.build();
		HttpPost httpPost = new HttpPost(url);
		if(list != null && !list.isEmpty()) {
			HttpEntity requestHttpEntity = new UrlEncodedFormEntity(list);
			httpPost.setEntity(requestHttpEntity);
		}
		CloseableHttpResponse res = client.execute(httpPost, context);
		return res;
	}
	
	public static CloseableHttpResponse get(String url) throws Exception {
		CloseableHttpClient client = HttpClients.custom()
				.setRedirectStrategy(new DefaultRedirectStrategy() {
					public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
						return false;
					}
				})
				.build();
		HttpGet httpGet = new HttpGet(url);
		CloseableHttpResponse res = client.execute(httpGet, context);
		return res;
	}
}
